//
//  FlutterMessageController.m
//  DemoiOSApplication
//
//  Created by  androidlongs on 2021/6/4.
//

#import "FlutterMessageController.h"
#import "Flutter/Flutter.h"
#import "FlutterPluginRegistrant/GeneratedPluginRegistrant.h"
#import "AppDelegate.h"

@interface FlutterMessageController ()
//用来接收Flutter中发送过来的消息
@property (weak, nonatomic) IBOutlet UILabel *reciLabel;
//用来加载显示Flutter Widget
@property (weak, nonatomic) IBOutlet UIView *showFlutterView;

@property (nonatomic,strong) FlutterBasicMessageChannel * messageChannel;

@end

@implementation FlutterMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取Flutter引擎
    FlutterEngine *flutterEngine = [(AppDelegate*)[UIApplication sharedApplication].delegate flutterEngine];
    self.messageChannel = [FlutterBasicMessageChannel messageChannelWithName:@"101" binaryMessenger:flutterEngine.binaryMessenger];
    
    __weak typeof(self) weakSelf = self;
    //接收消息监听
    [self.messageChannel setMessageHandler:^(id  _Nullable message, FlutterReply  _Nonnull callback) {
        NSLog(@" iOS 中接收到 ");
        weakSelf.reciLabel.text=[NSString stringWithFormat:@"%@", message];
        
        //发送一个回执
        callback(@"我是 ios 我接收到了你的信息了");
    }];
    
    
    
}
- (void)viewDidAppear:(BOOL)animated{
    //获取Flutter引擎
    FlutterEngine *flutterEngine = [(AppDelegate*)[UIApplication sharedApplication].delegate flutterEngine];
    //创建
    FlutterViewController *flutterViewController = [[FlutterViewController alloc] initWithEngine:flutterEngine nibName:nil bundle:nil];
    //设置View
    flutterViewController.view.frame =CGRectMake(0,0,300,400);
    //加载
    [self.showFlutterView addSubview:flutterViewController.view];
    
}

- (IBAction)sendMessagClick:(id)sender {
    
    [self.messageChannel sendMessage:@"qwer1234" reply:^(id  _Nullable reply) {
        NSLog(@" iOS 中接收到 flutter 中的回执 %@",reply);
    } ];
}

@end
